******************************************************************************************************
use "$data/demog_meta.dta", clear
keep respond as_of rate_spread loan_am

*tag loans that become hoepa post 2014
gen hoepa=0
replace hoepa=1 if rate_spread>=8.5&loan_am<50
replace hoepa=1 if rate_spread>=6.5&loan_am>=50

*count the total number of hoepa loans issued by each lender per year
sort respond as_of
by respond as_of: egen nb_hoepa=total(hoepa)


*count the total number of loans issued by each lender per year
sort respond as_of
gen count=1
by respond as_of: egen nb_loans=total(count)


*gen percent of loans that are hoepa
gen percent_hoepa=nb_hoepa/nb_loans


hist percent_hoepa if percent_hoepa~=0&as_of<2014, density ytitle(Percent of HOEPA Loans in Total Lender Portfolio Pre 2014)/*
*/ ytitle(density) graphregion(color(white)) bgcolor(white) xtitle("") color(navy*0.7)
graph export "$figures/figure4_panelc.eps", as(eps) preview(on) replace


hist percent_hoepa if percent_hoepa~=0&as_of>=2014, density ytitle(Percent of HOEPA Loans in Total Lender Portfolio Post 2014)/*
*/ ytitle(density) graphregion(color(white)) bgcolor(white) xtitle("") color(navy*0.7)
graph export "$figures/figure4_paneldspo.eps", as(eps) preview(on) replace

*distribution of hoepa issuer lenders including zero
hist percent_hoepa, density ytitle(Percent of HOEPA Loans in Total Lender Portfolio)/*
*/ ytitle(density) graphregion(color(white)) bgcolor(white) xtitle("") color(navy*0.7)
graph export "$figures/figurea12_panela.eps", as(eps) preview(on) replace


hist percent_hoepa if as_of<2014, density ytitle(Percent of HOEPA Loans in Total Lender Portfolio Pre 2014)/*
*/ ytitle(density) graphregion(color(white)) bgcolor(white) xtitle("") color(navy*0.7)
graph export "$figures/figurea12_panelb.eps", as(eps) preview(on) replace

hist percent_hoepa if as_of>=2014, density ytitle(Percent of HOEPA Loans in Total Lender Portfolio Post 2014)/*
*/ ytitle(density) graphregion(color(white)) bgcolor(white) xtitle("") color(navy*0.7)
graph export "$figures/figurea12_panelc.eps", as(eps) preview(on) replace






